﻿Public Class frmXemHoaDon
    Private cm As New Common
    Public dtThongTinTienHang As DataTable
    Public dtChiTietDonHangBan As DataTable
    Public dtThongTinKhachHang As DataTable
    Public dtNgay As DataTable
    Private Sub frmXemHoaDon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.WindowState = FormWindowState.Maximized
        crvHoaDon.Width = Me.Width
        crvHoaDon.Height = Me.Height
        Dim objReport As New rptHoaDon_Full
        objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
        objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
        objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
        objReport.Database.Tables("dtNgay").SetDataSource(dtNgay)
        crvHoaDon.ReportSource = objReport
    End Sub
    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Try
            Order_Print()
        Catch ex As Exception

        End Try
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        ' if it is a hotkey, return true; otherwise, return false
        Select Case keyData
            Case Keys.Control Or Keys.P
                'In hóa đơn
                Order_Print()
                Return True
            Case Else
                Exit Function
        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub Order_Print()
        Try
            crvIn.Width = Me.Width
            crvIn.Height = Me.Height
            Dim objReport As New rptHoaDon
            objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
            objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
            objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
            'Ngay
            Dim dsNgay As New dsNgay
            Dim dtNgay As New DataTable
            dtNgay = dsNgay.Tables(0)
            Dim dateRow As DataRow
            dateRow = dtNgay.NewRow
            dateRow.Item("Ngay") = dtpNgayHopDong.Value.ToShortDateString()
            formatDateVN2String_print(dateRow.Item("Ngay"))
            dtNgay.Rows.Add(dateRow)
            objReport.Database.Tables("dtNgay").SetDataSource(dtNgay)
            crvIn.ReportSource = objReport
            'config printer default
            objReport.PrintOptions.PrinterName = "HP Deskjet F2200 series"
            objReport.PrintToPrinter(1, False, 1, 1)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub dtpNgayHopDong_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpNgayHopDong.ValueChanged
        Try
            crvHoaDon.Width = Me.Width
            crvHoaDon.Height = Me.Height
            Dim objReport As New rptHoaDon_Full
            objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
            objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
            objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
            'Ngay
            Dim dsNgay As New dsNgay
            Dim dtNgay As New DataTable
            dtNgay = dsNgay.Tables(0)
            Dim dateRow As DataRow
            dateRow = dtNgay.NewRow
            dateRow.Item("Ngay") = dtpNgayHopDong.Value.ToShortDateString()
            formatDateVN2String_print(dateRow.Item("Ngay"))
            dtNgay.Rows.Add(dateRow)
            objReport.Database.Tables("dtNgay").SetDataSource(dtNgay)
            crvHoaDon.ReportSource = objReport
        Catch ex As Exception

        End Try
    End Sub
End Class